package com.haogu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haogu.entity.Function;
import com.haogu.entity.User;
import com.haogu.entity.VFunction;
import com.haogu.mapper.FunctionMapper;
import com.haogu.service.FunctionService;
import com.haogu.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
public class FunctionServiceImpl implements FunctionService {
    @Resource
    private FunctionMapper functionMapper;

    @Resource
    private UserService userService;

    @Override
    public List<Function> findByUserId(Integer id) {
        List<Function> byUserId = functionMapper.findByUserId(id);
        return byUserId;
    }

    @Override
    public List<Function> findTree(Integer parentId) {
        QueryWrapper<Function> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parentid",parentId);
        List<Function> functions = functionMapper.selectList(queryWrapper);
        return functions;
    }

    @Override
    public List<Function> finAll() {
        List<Function> functions = functionMapper.selectList(null);
        return functions;
    }

    /**
     * 通过角色id查找权限
     * @param id
     * @return
     */
    @Override
    public List<Function> findByRoleId(Integer id) {
        List<Function> byRoleId = functionMapper.findByRoleId(id);
        return byRoleId;
    }

    @Override
    public List<VFunction> selectAllFunction(Integer page, Integer size) {
//        System.out.println(functionMapper+"functionMapper");
        List<Function> functions = functionMapper.selectAllFunction((page - 1) * size, size);
        List<VFunction> vFunctions = new ArrayList<>();
        for(Function f : functions){
            VFunction vFunction = new VFunction();
            User u1= userService.findUserById(f.getCreateBy());
            User u2 = userService.findUserById(f.getUpdateBy());
            if(f.getId()!=null)
                vFunction.setId(f.getId());
            if(f.getFuncName()!=null)
                vFunction.setFuncName(f.getFuncName());
            if(f.getFuncType()!=null)
                vFunction.setFuncType(f.getFuncType());
            if(f.getFuncURL()!=null)
                vFunction.setFuncURL(f.getFuncURL());
            if(f.getFuncNote()!=null)
                vFunction.setFuncNote(f.getFuncNote());
            if(u1!=null&&u1.getUsername()!=null)
                vFunction.setCreateBy(u1.getUsername());
            if(f.getCreateTime()!=null)
                vFunction.setCreateTime(f.getCreateTime());


            if(u2!=null && u2.getUsername()!=null )
                vFunction.setUpdateBy(u2.getUsername());
            if(f.getUpdateTime()!=null)
                vFunction.setUpdateTime(f.getUpdateTime());
            if(f.getStatus()!=null)
                vFunction.setStatus(f.getStatus());
            vFunctions.add(vFunction);
        }
        return vFunctions;
    }

    @Override
    public Integer selectCount() {
        Integer integer = functionMapper.selectCount(null);
        return integer;
    }

    @Override
    public boolean insert(Function function) {
        int insert = functionMapper.insert(function);
        return insert==0?false:true;
    }

    @Override
    public boolean delete(Integer id) {
        int i = functionMapper.deleteById(id);
        return i==0?false:true;
    }

    @Override
    public Function selectFunctionById(Integer id) {
        Function function = functionMapper.selectById(id);
        return function;
    }

    @Override
    public boolean update(Function function) {
        int i = functionMapper.updateById(function);
        return i==0?false:true;
    }
}
